package com;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.CookieHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.ClusteredSessionStore;
import io.vertx.ext.web.sstore.SessionStore;
import io.vertx.ext.web.sstore.impl.ClusteredSessionStoreImpl;
import io.vertx.ext.web.sstore.impl.SessionImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Ldj
 * @Date 2018/3/19 17:38
 * @Description 数据处理
 */
public class ApiVerticle extends AbstractVerticle {


    private List list = new ArrayList();

    @Override
    public void start(){
        Router router = Router.router(vertx);
        router.route().handler(CookieHandler.create());
        router.route().handler(SessionHandler.create(ClusteredSessionStore.create(vertx)));

        router.get("/").handler(rc -> {
            rc.response().end("Welcome");
        });
        router.get("/names").handler(
                rc -> {
                    rc.session().put("ccc","bbbbb");
                    rc.response()
                            .putHeader("content-type", "application/json")
                            .end("服务1");
                });

        router.get("/s1").handler(
                rc -> {
                    System.out.println(JsonObject.mapFrom(rc.session()).toString());
                    String s = rc.session().get("ccc");
                    rc.response()
                            .putHeader("content-type", "application/json")
                            .end(s);
                });
        Future<HttpServer> httpServerFuture = Future.future();
        vertx.createHttpServer()
                .requestHandler(router::accept)
                .listen(8083, httpServerFuture.completer());
    }
}
